import { request } from '../../request/index.js'
import { requestPayment, showToast } from '../../utils/asyncWx.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsObj: {}
  },
  orderId: 0,
  goodsId: '',

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { orderId, goodsId } = options
    this.orderId = orderId
    this.goodsId = goodsId
    this.getGoodsDetail()
  },
  //获取商品详细数据
  async getGoodsDetail() {
    const goodsId = this.goodsId
    const token = wx.getStorageSync("token");
    const header = { token }
    const res = await request({ url: '/maintenance/goods/' + goodsId + '/showList', data: { goodsId }, header })
    this.setData({
      goodsObj: res.data
    })
  },
  //去支付预约金
  async handlePay() {
    try {
      const token = wx.getStorageSync("token");
      const header = { token }
      const orderId = parseInt(this.orderId)
      const queryParams = { orderPaymentMethod: 1, orderId }
      const res = await request({ url: '/order/maintenance/' + orderId + '/payment', data: { queryParams }, method: 'PUT', header })
      console.log(res)
      const paymentResult = res.data

      const pay = {
        nonceStr: paymentResult.nonceStr,
        package: paymentResult.packageValue,
        paySign: paymentResult.paySign,
        signType: paymentResult.signType,
        timeStamp: paymentResult.timeStamp
      }
      await requestPayment(pay)

      await showToast({ title: '支付成功' })
      // wx.navigateTo({
      //   url: '../maintenance-order/index?type=3'
      // });
    } catch (error) {
      await showToast({ title: '订单已取消' })
      // wx.navigateTo({
      //   url: '../maintenance-order/index?type=1'
      // });
      console.log(error)
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})